package com.example.app_test;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ServerConnect.GetInfo;
import ServerConnect.GetWeather;
import ServerConnect.QueryTask;
import android.app.Activity;
import android.app.DownloadManager.Query;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class LoginSuccess extends Activity{
	
	private TextView textview = null;
	private GetWeather getWeather = new GetWeather();
	String httpUrl = "http://apis.baidu.com/apistore/weatherservice/weather?citypinyin=";
	String httpArg = "beijing";
	private String result;
	private int flag = 0;
	String city;
	static String apikey = "6811c9c27bb1bb33a52ee457a4f10da6";
	private QueryTask queryTask = new QueryTask();
	private String output = null;
	private GetInfo getInfo = new GetInfo();
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_success);
		textview = (TextView)findViewById(R.id.text_view);
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		String user  = bundle.getString("user");
		city = getInfo.get_usercity(user);
		System.out.println(user + city);
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
					
					LoginSuccess.this.ShowWeather(city);
				
				
			}

		}, 1000);
	}
	public void ShowWeather(String city){
		flag = 0;
		httpArg = city;
		System.out.println(httpUrl+httpArg);
		
		Thread thread = new Thread(){
			public void run(){
				result  = getWeather.request(httpUrl, httpArg);
				if (result != null) {
		            try {
		                JSONObject jsonObject = new JSONObject(result);
		                String resultCode = jsonObject.getString("errNum");
		                System.out.println(resultCode);
		                if (resultCode.equals("0")) {
		                    
		                    String retData = jsonObject.getString("retData");
		                    JSONObject resultJsonObject = new JSONObject(retData);
		                    output = "city" + ": " + resultJsonObject.getString("city") + "\n"
		                            + "date" + ": " + resultJsonObject.getString("date") + "\n"
		                            + "time" + ": " + resultJsonObject.getString("time") + "\n"
		                            + "weather" + ": " + resultJsonObject.getString("weather") + "\n"
		                            + "tmp" + ": " + resultJsonObject.getString("temp") + "\n"
		                            + "low" + ": " + resultJsonObject.getString("l_tmp") + "\n"
		                            + "hith" + ": " + resultJsonObject.getString("h_tmp") + "\n"
		                            + "WD" + ": " + resultJsonObject.getString("WD") + "\n"
		                            + "WS" + ": " + resultJsonObject.getString("WS") + "\n"
		                            + "sunrise" + ": " + resultJsonObject.getString("sunrise") + "\n"
		                            + "sunset" + ": " + resultJsonObject.getString("sunset") + "\n";
		                   
		                } else if (!resultCode.equals("0")) {
		                    String reason = jsonObject.getString("reason");
		                   
		                } else {
		                    
		                  
		                }
		            } catch (JSONException e) {
		                // TODO Auto-generated catch block
		                e.printStackTrace();
		            }
		        } else {
		          
		            
		        }
				
				
				flag = 1;
			}
		};
		thread.start();
		while(flag == 0){
			
		}
		textview.setText(output);
		
		System.out.println(result);
		//textview.setText(result);
	}
	
	public void Back(View view){
		super.onBackPressed();
	}
	
}
